//
//  HHCountdownViewController.m
//  HaoHint
//
//  Created by winter on 16/5/22.
//  Copyright © 2016年 wd. All rights reserved.
//

#import "CountdownViewController.h"
#import "CountdownTableViewCell.h"
#import "SetCountdownViewController.h"
#import "CountdownModel.h"
#import "DataTool.h"

@interface CountdownViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

/**  */
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation CountdownViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"倒计时";
    
    self.dataArray = [NSMutableArray array];
    
    [self setupNavBarItem];
    [self setupSubViews];
    
    [self loadData];
}

- (void)setupNavBarItem
{
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addButtonAction)];
    
    [self.navigationItem setRightBarButtonItem:rightItem];
}

- (void)setupSubViews
{
    self.tableView.tableFooterView = [UIView new];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CountdownTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
}

- (UIView *)tableViewHeader
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 44)];
    view.backgroundColor = kBackGroundColor;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 100, 44)];
    label.text = @"已完成";
    label.font = [UIFont systemFontOfSize:14];
    [view addSubview:label];
    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(kScreen_Width - 40, 12, 20, 20)];
    imageView.image = [UIImage imageNamed:@"task_icon_arrow"];
    [view addSubview:imageView];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = view.bounds;
    button.backgroundColor = [UIColor clearColor];
    [view addSubview:button];
    return view;
}

- (void)loadData
{
    NSArray *datas = [DataTool readData];
    if (datas.count == 0) {
        return;
    }
    [self.dataArray addObjectsFromArray:datas];
//    for (int i = 0; i < 2; i++) {
//        CountdownModel *model = [[CountdownModel alloc] init];
//        model.timeTitle = @"倒计时1";
//        model.soundName = @"bell";
//        model.countdownTime = @"00:00:30";
//        model.remindEarlyType = CountdownModelRemindStyle_Early1Min;
//        
//        [self.dataArray addObject:model];
//    }
    [self.tableView reloadData];
    if (self.dataArray.count > 1) {
        self.navigationItem.rightBarButtonItem.enabled = NO;
    }
}

#pragma mark - button action

- (void)editTableView:(UIButton *)sender
{
    self.tableView.editing = !sender.isSelected;
    sender.selected = !sender.isSelected;
}

- (void)addButtonAction
{
    __weak typeof(self) weakSelf = self;
    [self setCountdownTime:nil block:^(CountdownModel *blockModel) {
        [weakSelf.dataArray addObject:blockModel];
        [weakSelf.tableView reloadData];
        if (weakSelf.dataArray.count > 1) {
            weakSelf.navigationItem.rightBarButtonItem.enabled = NO;
        }
        [DataTool saveData:weakSelf.dataArray];
    }];
}

- (void)setCountdownTime:(CountdownModel *)model block:(void (^)(CountdownModel *blockModel))block
{
    SetCountdownViewController *vc = [[SetCountdownViewController alloc] initWithNibName:@"SetCountdownViewController" bundle:nil];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
    [self presentViewController:nav animated:YES completion:nil];

    vc.countdownModel = model;
    vc.setCountdownBlock = block;
}

#pragma mark - tabview ddatasoure & delegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CountdownTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.countdownModel = self.dataArray[indexPath.section];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    CountdownModel *countdownModel = self.dataArray[indexPath.section];
    
    if (countdownModel.isCountdowning) {
        return;
    }
    
    __weak typeof(self) weakSelf = self;
    [self setCountdownTime:countdownModel block:^(CountdownModel *blockModel) {
        [weakSelf.dataArray replaceObjectAtIndex:indexPath.section withObject:blockModel];
        [weakSelf.tableView reloadData];
        [DataTool saveData:weakSelf.dataArray];
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    return 20;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CountdownModel *countdownModel = self.dataArray[indexPath.section];
    if (countdownModel.isCountdowning) {
        return UITableViewCellEditingStyleNone;
    }
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.dataArray removeObjectAtIndex:indexPath.section];
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
        [tableView deleteSections:[[NSIndexSet alloc] initWithIndex:indexPath.section] withRowAnimation:UITableViewRowAnimationTop];
        if (self.dataArray.count < 2) {
            self.navigationItem.rightBarButtonItem.enabled = YES;
        }
        [DataTool saveData:self.dataArray];
    }
}
@end
